package com.songcan.oauth.compent;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.songcan.common.vo.MenuVo;
import com.songcan.common.vo.UserDetailsNew;
import com.songcan.model.oauth.SysDeptUser;
import com.songcan.model.oauth.SysMenu;
import com.songcan.model.oauth.SysUser;
import com.songcan.oauth.service.SysDeptService;
import com.songcan.oauth.service.SysDeptUserService;
import com.songcan.oauth.service.SysMenuService;
import com.songcan.oauth.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


@Service
@Slf4j
public class OauthUserDetailsService implements UserDetailsService {

    private  RedisTemplate redisTemplate;
    private  SysUserService sysUserService;
    private  SysMenuService sysMenuService;
    private  SysDeptUserService sysDeptUserService;

    @Autowired
    public OauthUserDetailsService(RedisTemplate redisTemplate,SysUserService sysUserService, SysDeptUserService sysDeptUserService,
                                   SysMenuService sysMenuService){
        this.redisTemplate = redisTemplate;
        this.sysUserService = sysUserService;
        this.sysMenuService = sysMenuService;
        this.sysDeptUserService = sysDeptUserService;
    }

    @Override
    public UserDetails loadUserByUsername(String s)  {

        log.info("登陆用户名:{}",s);
        UserDetailsNew userDetailsNew = null;
        SysUser user  = sysUserService.getOne(Wrappers.lambdaQuery(new SysUser())
        .eq(SysUser::getUsername,s));

        if(Objects.isNull(user)){
            return  null;
        }
        Boolean isEnabled = user.getEnabled()==1;
        List<String> jsonObjects = sysUserService.selectByDeptRole(user.getUserId());


        Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
        for (String p : jsonObjects) {
            if(StrUtil.isNotEmpty(p)) {
                grantedAuthorities.add(new SimpleGrantedAuthority(p));
            }
        }
        Set<String> organizations = new HashSet<>();
        List<SysDeptUser> list1 = sysDeptUserService.list(Wrappers.lambdaQuery(new SysDeptUser()).eq(SysDeptUser::getUserId,user.getUserId()));
        if(Objects.nonNull(list1)&&list1.size()>0){
            organizations = list1.parallelStream().map(t->String.valueOf(t.getDeptId())).collect(Collectors.toSet());
        }
        userDetailsNew =  new UserDetailsNew(s,user.getSalt(),user.getPassword(),grantedAuthorities,isEnabled,user.getUserId(),user.getLockFlag() == 0,user.getName());
        userDetailsNew.setOrganizations(organizations);
        return userDetailsNew;
    }
}
